package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.PurchaseListGoodsMapper;
import com.atguigu.jxc.dao.PurchaseListMapper;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class PurchaseListGoodsServiceImpl
        extends ServiceImpl<PurchaseListGoodsMapper, PurchaseListGoods>
        implements PurchaseListGoodsService {

    @Autowired
    private PurchaseListMapper purchaseListMapper;

    @Autowired
    private PurchaseListGoodsMapper purchaseListGoodsMapper;


    @Override
    public Map<String, Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<PurchaseListGoods> purchaseListList = purchaseListGoodsMapper.list(purchaseNumber, supplierId, state, sTime, eTime);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", purchaseListList);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer purchaseListId) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<PurchaseListGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_list_id", purchaseListId);
        List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsMapper.selectList(queryWrapper);
        map.put("rows", purchaseListGoodsList);
        return map;
    }

    @Override
    @Transactional
    public ServiceVO delete(Integer purchaseListId) {
        //删除进货单商品列表
        QueryWrapper<PurchaseListGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_list_id", purchaseListId);
        purchaseListGoodsMapper.delete(queryWrapper);

        //删除进货单
        QueryWrapper<PurchaseList> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("purchase_list_id", purchaseListId);
        purchaseListMapper.delete(queryWrapper1);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


}
